* The analysis was conducted using Stata 16. 
clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\DSBC"
use DatasetDSBC, clear

*Need to repeat all the analyses for the four conditions in this dataset separately (Time Pressure vs. No Pressure, Pie vs. Bar Lottery representation)
*The conditions are identified by the variable Pie:[0,1] and nopressure:[0,1].
******************
keep if nopressure==1
keep if Pie==0
*******************

*This is an out-of-sample estimation, only keep comparisons with idpair ==5 as described in the paper.
keep if idleft==5 | idright==5

*Summarize the dataset
sum prob1 prob2 out1 out2 choiceleft

*transform choice dummy from 0,1 to -1,1
replace choiceleft=-1 if choiceleft==0
tab choiceleft
save BaseEst, replace

*Start the inizialization of the ML program
clear
program drop _all
program define ML_eut0

	* specify the arguments of this program
	args lnf r noise
		
	* declare the temporary variables to be used
	tempvar euL euR uL1 uL2 uR1 uR2 choicea probl prob2 out1 out2 maxOut euDiff
	
	* please do not display all these steps on the screen for every iteration
	quietly {

	
	*choiceA probAl probBl outA1 outB1 maxOut probA2 probB2 outA2 outB2
		generate int    `choicea' = $ML_y1
		generate double `probl' = $ML_y2 
		generate double `prob2' = $ML_y3 
		generate double `out1' = $ML_y4
		generate double `out2' = $ML_y5
		generate double `maxOut' = $ML_y6

		*calculate utility each option usinf CARA, plug in here different utility functions (e.g. CRRA) for alternative estimations. 
		*for RPM one just needs to ocmpare the risk attitude which would make a DM indifferent for each lottery pair with the current best estimate of the risk attitude. Can skip the next two passages.
		generate double `uL1' = ((1-exp(-`r'*`out1'))/(1-exp(-`r'*(`maxOut'))))
		generate double `uR1' = ((1-exp(-`r'*`out2'))/(1-exp(-`r'*(`maxOut'))))
		
		*calculate EU of each lottery		
		generate double `euL' = (`probl'*`uL1')
		generate double `euR' = (`prob2'*`uR1')

		*difference EU divided by noise
		generate double `euDiff' = (`euL' - `euR')/`noise'

		*evaluate the likelihood choice based on EU diff
		replace `lnf' = ln(normal( `euDiff')) if `choicea'==1
		replace `lnf' = ln(normal(-`euDiff')) if `choicea'==-1
	}

	
end


*Loop estimation over participants.
local i = 1
while `i' < 61 {
display `i'
use BaseEst, clear
egen m1 = max(out1)
egen m2 = max(out2)
gen maxOut=max(m1,m2)
drop m1 m2
 
keep if subj==`i'

sum choicea prob1 prob2 out1 out2 maxOut
gen est_r=.
gen noise=.

display `i'
capture: ml model lf ML_eut0 (r: choicea prob1 prob2 out1 out2  = ) (noise: ), display  maximize
matrix b = e(b)  
matrix list b  
replace est_r= b[1,1]  
replace noise = b[1,2]  
matrix drop b

*keep only subjects' identifier, risk attitudes, and noise parameter.
keep  subj est_r noise 
collapse subj est_r noise Pie nopressure
***************************************************************
*Change the name of these variables for each conditions!
***************************************************************
*rhat_YesP_Bars rhat_NoP_Bars rhat_YesP_Pies rhat_NoP_Pies
*noise_YesP_Bars noise_NoP_Bars noise_YesP_Pies noise_NoP_Pies
gen rhat_NoP_Pies = est_r
gen noise_NoP_Pies = noise
drop est_r noise
***************************************************************
*Change the name of this dataset for each utilit function or noise specification!
***************************************************************
 save individualiskNoTimePressureBarsCARAsubj`i', replace
local i = `i' + 1
}

*Merge individual datasets into one.
use individualiskNoTimePressureBarsCARAsubj1, clear
local i = 2
while `i' < 61 {
append using individualiskNoTimePressureBarsCARAsubj`i'
local i = `i' + 1
}
save individualiskNoTimePressureBarsCARA, replace

*Repeat the same procedure for RUM-CRRA, RPM-CARA, and RPM-CRRA.
*Repeat the same procedure for the four conditions: Time Pressure vs. No Pressure, Pie vs. Bar Lottery representation.